/*
 * Created on 27.08.2006 by leck
 *
 */
package de.devisnik.eidle.util;

import java.io.IOException;
import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

import de.devisnik.eidle.EidleActivator;

public class ScreenUtils {

	public static void setCursorInvisible(final Control control) {
		final Cursor cursor = createTransparentCursor(control.getDisplay());
		control.setCursor(cursor);
	}

	private static Cursor createTransparentCursor(final Display display) {
		// create a cursor with a transparent image
		final Color white = display.getSystemColor(SWT.COLOR_WHITE);
		final Color black = display.getSystemColor(SWT.COLOR_BLACK);
		final PaletteData palette = new PaletteData(new RGB[] { white.getRGB(),
				black.getRGB() });
		final ImageData sourceData = new ImageData(16, 16, 1, palette);
		sourceData.transparentPixel = 0;
		final Cursor cursor = new Cursor(display, sourceData, 0, 0);
		return cursor;
	}

	public static Image takeScreenShot(final Display display) {
		final GC gc = new GC(display);

		// we take a snap shot of the primary monitor only!
		final Rectangle displayBounds = display.getPrimaryMonitor().getBounds();
		// application window keeps a one pixel border, so we cut that off from
		// screen image
		final Rectangle bounds = new Rectangle(0, 0, displayBounds.width - 2,
				displayBounds.height - 2);
		final Image screenShotImage = new Image(display, bounds);
		gc.copyArea(screenShotImage, 1, 1);
		gc.dispose();
		return screenShotImage;
	}

	public static Image takeSnapShot(final Control scrollable) {
		final GC gc = new GC(scrollable);
		final Image screenShotImage = new Image(scrollable.getDisplay(),
				scrollable.getBounds());
		gc.copyArea(screenShotImage, 0, 0);
		gc.dispose();
		return screenShotImage;
	}

	private static Image itsScreenImage;

	public static Image getScreenImage() {
		if (itsScreenImage == null) {
			return getFallbackImage();
		}
		return itsScreenImage;
	}

	public static void setScreenImage(final Image image) {
		itsScreenImage = image;
	}

	private static Image getFallbackImage() {
		final URL internalURL = FileLocator.find(EidleActivator.getDefault()
				.getBundle(), new Path("splash.bmp"), null);
		try {
			final URL resolvedURL = FileLocator.resolve(internalURL);
			return new Image(PlatformUI.getWorkbench().getDisplay(),
					resolvedURL.getFile());
		} catch (final IOException e) {
			e.printStackTrace();
			return ImageDescriptor.createFromURL(internalURL).createImage();
		}
	}
}
